﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;

namespace WeChartWorkRobot.Service.Jobs
{

    [Serializable]
    public class SimpleTrigger : TriggerBase
    {
        private readonly int _intevalSecond;
        private readonly int _repeat;

        /// <summary>
        /// 定时触发
        /// </summary>
        /// <param name="intevalSecond">触发频率（秒）</param>
        /// <param name="repeat">重复次数（-1为不限），实际运行次数为：重复次数+1</param>
        public SimpleTrigger(int intevalSecond, int repeat)
        {
            _intevalSecond = intevalSecond;
            _repeat = repeat;
        }

        protected internal override ITrigger GetQuartzTrigger()
        {
            
            return TriggerBuilder.Create()
               .StartAt(DateTimeOffset.Now.AddSeconds(_intevalSecond))
               .WithSimpleSchedule(x =>
               {
                   var xx = x.WithIntervalInSeconds(_intevalSecond);
                   if (_repeat < 0)
                   {
                       xx.RepeatForever();
                   }
                   else
                   {
                       xx.WithRepeatCount(_repeat);
                   }
               }).Build();
        }

    }
}
